package com.dyz.swordFingerOffer;

import java.util.Stack;

/**
 * @author: daiyizheng
 * @date: 2021/6/4 23:21
 * @description:
 */
public class IsPopOrder31 {
    public static void main(String[] args) {
        int [] pushA = new int[]{1,2,3,4,5};
        int [] popA = new int[]{1,2,3,4,5};
        boolean a = new IsPopOrder31().IsPopOrder(pushA,popA);
        System.out.println(a);
    }
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        //通过栈的方式
        //如果栈中没有，加入栈中
        if (pushA==null||pushA.length==0||popA==null||popA.length==0 ||
                pushA.length!=popA.length){
            return false;
        }

        Stack<Integer> stack = new Stack<>();
        stack.push(pushA[0]);
        int j = 1;
        for(int i=0; i<popA.length;i++){
            int num = popA[i];
            while((stack.isEmpty() || stack.peek()!=num) && j<pushA.length){
                stack.push(pushA[j++]);
            }
            if(stack.peek()==num){
                stack.pop();
                continue;
            }
            return false;
        }
        return true;
    }
}
